﻿/* 
 *  <copyright file="PlatformerScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using SEGameExample.Kludge;
using SEGameExample.InputMappers;
using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Components.Trigger.Response;
using SobrietyEngine.Logging;
using SobrietyEngine.Scene;
using SobrietyEngine.Screens;

namespace SEGameExample.Screens
{
    class UglyPlatformer: BaseLevel
    {
        public UglyPlatformer():base("PlatformerScreen")
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            //set some options
            //turn on logging of some events
            EventRouter.LogRaisedEvents = true;
            EventRouter.LogEventIgnore.Add("Input.");
            EventRouter.LogEventIgnore.Add(".Move");
            EventRouter.LogEventIgnore.Add(".Rotate");
            EventRouter.LogEventIgnore.Add(".ActorInit");

            //set some logging options
            Logger.AppendLoggingType(Logger.LogType.AssertOnError |
                                     Logger.LogType.VSConsole |
                                     Logger.LogType.RaiseLogEvent);

            //set up our PlayerOne (first found user with an Xbox 360 controller)
            //turn off any controllers that may be active (shoudl always disable controllers when you are finished!)
            PlayerOne.DisableAllInputMappers();
            if (!PlayerOne.HasInputMapper(typeof(PlatformerEventMapper)))
                PlayerOne.AddInputMapper(new PlatformerEventMapper(EventRouter, PlayerOne.Guid));
            PlayerOne.EnableInputMapper(typeof(PlatformerEventMapper));

            //lets test level loading
            GameplaySpawner.Level(Scene, "Meshes/levelThing2", ScreenManager.Game);
      
            //test animated textured quad with PlayerOne control, hit boxes, player object
            IActor playerActor = GameplaySpawner.PlatformerPlayer2D(Scene, new Vector2(-3, 2), PlayerOne.Guid, ScreenManager.GraphicsDevice, FarseerPhysics.Dynamics.Category.Cat1, textureManager);

            //create 3D Scene camera that trys to follow the player
            IActor actor = GameplaySpawner.OrthoFollowActorCam3D(Scene, ScreenManager.FullScreenViewport, ScreenManager.GraphicsDevice, playerActor.Guid);

            //lets create a simple rectangle trigger and link it to
            //a debug trigger component, so we can test triggers.
            //move to trigger one of the platforms above to turn on/off
            IActor trigger = GameplaySpawner.createRectanglePressureTrigger(Scene, new Vector2(-2, 1.5f), new Vector2(2, 2), "TestTrigger");
            ITrigger iTrigger = trigger.Get<ITrigger>();
            
            //lets add the triggerable component to the trigger (it responds via events, so its an acceptable test)
            trigger.AttachComponent(new TriggerResponseBase( new String[] {iTrigger.TriggerName}));
            
            //Have to init and reigseter the compnent with the Scene manually, since we have already initialized and registered
            //the noNewline on the Scene.
            Scene.RegisterComponent(trigger.Get<TriggerResponseBase>());            

            //lets try to create several moving platforms, weee
            IActor firstPlatform = null;
            for (int a = 1; a < 10; ++a)
            {
                Vector2 size = new Vector2(3, 0.25f);
                
                Vector2 p0 = new Vector2(1, 1.75f) * a; 
                Vector2 p1 = new Vector2(3, 1.75f) * a;
                Vector2[] pathVecs = new Vector2[] { p0, p1 };
                List<IActor> path = GameplaySpawner.createCirclePath(Scene, pathVecs); 

                actor = GameplaySpawner.createMovingPlatform(Scene,size, p0, path[0].Guid, a % 2 == 0);
                if(a ==1)
                    firstPlatform = actor;
            }

            //lets make a crazy zig zag path platform to see wtf happens
            Vector2[] pathVecs2 = new Vector2[] { new Vector2(1, 1), new Vector2(-3, 5), new Vector2(2, 2), new Vector2(-4, 1) };
            List<IActor> nodeActors = GameplaySpawner.createCirclePath(Scene, pathVecs2);
            actor = GameplaySpawner.createMovingPlatform(Scene, new Vector2(2.5f, 0.25f), new Vector2(1, 1), nodeActors[0].Guid, false);

            //add a movement toggle trigger response to one of the moving platforms
            IComponent comp = new ToggleMoverActiveCmp(new string[] { iTrigger.TriggerName }, true);
            firstPlatform.AttachComponent(comp);
            
            //Have to init and reigseter the compnent with the Scene manually, since we have already initialized and registered
            //the noNewline on the Scene.
            Scene.RegisterComponent(comp);

             
            //Test some gui and hud stuff
            //create a 2D gui camera (for interesting gui effects)       
            //IActor cam2D = GUISpawner.Camera2D(Scene, ScreenManager.FullScreenViewport);
            IActor cam2d = SobrietyEngine.Actor.Builders.Cameras.GuiCameraBldr.Build(Scene, ScreenManager.FullScreenViewport);

            //some help text about controls
            String help = "This is a hideous 2.5D platformer. \n ";
            help += "Left thumbstick moves\n";
            help += "(A) jumps, (RB) super jumps, \n";
            help += "(B) exits, (Y) toggles physics debug \n";
            help += "(LB) toggle debug console\n";

            SobrietyEngine.Actor.Builders.GUI.Text2D.Build(Scene, new Vector2(0, ScreenManager.FullScreenViewport.Height / 2), help);               
           
            //lets see if we can make some triggerable text
            actor = SobrietyEngine.Actor.Builders.GUI.Text2D.Build(Scene, new Vector2(300, 300), "GUI Text triggered by a trigger and sensor box");
            actor.AttachComponent(new ToggleVisibilityCmp(new string[] { iTrigger.TriggerName } ));
            Scene.RegisterComponent(actor.Get<ToggleVisibilityCmp>());


            //lets try to create a goomba strType moving object
            actor = GameplaySpawner.CreateGoomba(Scene, new Vector2(3, 3), 0.5f, new Vector2(0.01f, 0));


            //controllers handle all the movement and thining - AI, PlayerOne input, network input, etc            
            SceneGameService.Scenes[SceneName].EnableControllers();
        }

    }
}